let state, SNO;


layui.use(['table', 'jquery', 'upload'], function () {
    let table = layui.table
        , $ = layui.jquery
        , layer = layui.layer
        , upload = layui.upload
    //展示初稿数据
    table.render({
        elem: '#paper-info1'
        , toolbar: true
        , title: '论文信息表'
        , url: '/getPaper'
        , page: true
        , where: {userName: sno, state: "初稿", type: 1}
        , even: true
        , cols: [[ //标题栏
            {field: 'bh', title: '编号', sort: true, hide: true}
            , {field: 'title', title: '课题名称', width: 200}
            , {field: 'sname', title: '学生姓名', width: 100}
            , {field: 'sno', title: '学生学号', width: 150}

            , {
                field: 'tno1', title: '教师姓名', width: 100, templet: function (res) {
                    return res.title1.tName;
                }
            }
            , {field: 'tno', title: '教师工号', width: 150}
            , {field: 'state', title: '论文阶段', width: 100}
            , {
                field: 'state1', title: '审核状态', width: 100, align: 'center', templet: function (res) {
                    if (res.stageCheck === null) {
                        return '<span style="color: red">待提交</span>'
                    } else {
                        if (res.stageCheck.state === '审核中') {
                            return '<span style="color: orange">审核中</span>'
                        } else if (res.stageCheck.state === '审核完成') {
                            return '<span style="color: green">审核完成</span>'
                        }
                    }
                }
            }
            , {
                field: 'state1', title: '是否通过', width: 100, align: 'center', templet: function (res) {
                    if (res.stageCheck === null) {
                        return '<span class="layui-badge layui-bg-red">待提交</span>'
                    } else {
                        if (res.stageCheck.isPass === null || res.stageCheck.isPass === "") {
                            return '<span  class="layui-badge layui-bg-orange">审核中</span>'
                        } else if (res.stageCheck.isPass === '通过') {
                            return '<span class="layui-badge layui-bg-green">通过</span>'
                        } else if (res.stageCheck.isPass === '不通过') {
                            return '<span class="layui-badge " style="background-color: gray">不通过</span>'
                        }
                    }
                }
            }
            , {fixed: 'right', title: '课题详情', toolbar: '#title-tool', align: 'center', width: 100}
            , {fixed: 'right', title: '操作', toolbar: '#paper-info-tool', width: 150}
        ]]
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });
    //展示定稿数据
    table.render({
        elem: '#paper-info2'
        , toolbar: true
        , title: '论文信息表'
        , url: '/getPaper'
        , page: true
        , where: {userName: sno, state: "定稿", type: 1}
        , even: true
        , cols: [[ //标题栏
            {field: 'bh', title: '编号', sort: true, hide: true}
            , {field: 'title', title: '课题名称', width: 200}
            , {field: 'sname', title: '学生姓名', width: 100}
            , {field: 'sno', title: '学生学号', width: 150}

            , {
                field: 'tno1', title: '教师姓名', width: 100, templet: function (res) {
                    return res.title1.tName;
                }
            }
            , {field: 'tno', title: '教师工号', width: 150}
            , {field: 'state', title: '论文阶段', width: 100}
            , {
                field: 'state1', title: '审核状态', width: 100, align: 'center', templet: function (res) {
                    if (res.stageCheck === null) {
                        return '<span style="color: red">待提交</span>'
                    } else {
                        if (res.stageCheck.state === '审核中') {
                            return '<span style="color: orange">审核中</span>'
                        } else if (res.stageCheck.state === '审核完成') {
                            return '<span style="color: green">审核完成</span>'
                        }
                    }
                }
            }
            , {
                field: 'state1', title: '是否通过', width: 100, align: 'center', templet: function (res) {
                    if (res.stageCheck === null) {
                        return '<span class="layui-badge layui-bg-red">待提交</span>'
                    } else {
                        if (res.stageCheck.isPass === null || res.stageCheck.isPass === "") {
                            return '<span  class="layui-badge layui-bg-orange">审核中</span>'
                        } else if (res.stageCheck.isPass === '通过') {
                            return '<span class="layui-badge layui-bg-green">通过</span>'
                        } else if (res.stageCheck.isPass === '不通过') {
                            return '<span class="layui-badge " style="background-color: gray">不通过</span>'
                        }
                    }
                }
            }
            , {fixed: 'right', title: '课题详情', toolbar: '#title-tool', width: 100}
            , {fixed: 'right', title: '操作', toolbar: '#paper-info-tool', width: 150}
        ]]
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });
    //展示终稿数据
    table.render({
        elem: '#paper-info3'
        , toolbar: true
        , title: '论文信息表'
        , url: '/getPaper'
        , page: true
        , where: {userName: sno, state: "终稿", type: 1}
        , even: true
        , cols: [[ //标题栏
            {field: 'bh', title: '编号', sort: true, hide: true}
            , {field: 'title', title: '课题名称', width: 200}
            , {field: 'sname', title: '学生姓名', width: 100}
            , {field: 'sno', title: '学生学号', width: 150}

            , {
                field: 'tno1', title: '教师姓名', width: 100, templet: function (res) {
                    return res.title1.tName;
                }
            }
            , {field: 'tno', title: '教师工号', width: 150}
            , {field: 'state', title: '论文阶段', width: 100}
            , {
                field: 'state1', title: '审核状态', width: 100, align: 'center', templet: function (res) {
                    if (res.stageCheck === null) {
                        return '<span style="color: red">待提交</span>'
                    } else {
                        if (res.stageCheck.state === '审核中') {
                            return '<span style="color: orange">审核中</span>'
                        } else if (res.stageCheck.state === '审核完成') {
                            return '<span style="color: green">审核完成</span>'
                        }
                    }
                }
            }
            , {
                field: 'state1', title: '是否通过', width: 100, align: 'center', templet: function (res) {
                    if (res.stageCheck === null) {
                        return '<span class="layui-badge layui-bg-red">待提交</span>'
                    } else {
                        if (res.stageCheck.isPass === null || res.stageCheck.isPass === "") {
                            return '<span  class="layui-badge layui-bg-orange">审核中</span>'
                        } else if (res.stageCheck.isPass === '通过') {
                            return '<span class="layui-badge layui-bg-green">通过</span>'
                        } else if (res.stageCheck.isPass === '不通过') {
                            return '<span class="layui-badge " style="background-color: gray">不通过</span>'
                        }
                    }
                }
            }
            , {fixed: 'right', title: '课题详情', toolbar: '#title-tool', width: 100}
            , {fixed: 'right', title: '操作', toolbar: '#paper-info-tool', width: 150}
        ]]
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });
    //监听初稿表工具事件
    table.on('tool(paper-info1)', function (obj) {
        let data = obj.data;
        state = "初稿";
        SNO = data.sno;
        if (obj.event === 'taskDetail') {
            layer.open({
                type: 1
                , offset: 50
                , shade: [0.3, '#000']
                , shadeClose: true
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '学生课题详情'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setTaskDetail(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        } else if (obj.event === 'edit') {
            if (data.state !== "初稿") {
                layer.msg("未处在该阶段，无法提交");
                return false;
            } else if (data.stageCheck === null || data.stageCheck.isPass === "不通过") {
                let page = layer.open({
                    type: 1
                    , offset: 10
                    , area: [700, 550]
                    , content: $("#paperUpload-box")
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        //数据回显
                        setMsg(data);
                    }
                    , end: function () {
                        $("#paperUpload-box").css("display", 'none');
                    }
                });
            } else if (data.stageCheck.state === "审核中") {
                layer.msg("已提交，不可重复提交");
                return false;
            }

        } else if (obj.event === 'query') {
            if (data.stageCheck === null) {
                layer.msg("暂无内容，无法查看");
            } else {
                layer.open({
                    type: 1
                    , offset: 50
                    , shade: [0.3, '#000']
                    , shadeClose: true
                    , area: ['840px', '600px']
                    , content: $("#paper-detail")
                    , title: '论文审核详情'
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        /*设置回显数据*/
                        setPaperDetail(data);
                    }
                    , end: function () {
                        $("#paper-detail").css("display", 'none');
                    }
                });
            }
        }
    });
    //监听定稿工具事件
    table.on('tool(paper-info2)', function (obj) {
        let data = obj.data;
        SNO = data.sno;
        state = "定稿";
        if (obj.event === 'taskDetail') {
            layer.open({
                type: 1
                , offset: 50
                , shade: [0.3, '#000']
                , shadeClose: true
                , skin: 'layui-layer-rim'
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '论学生课题详情'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setTaskDetail(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        } else if (obj.event === 'edit') {
            if (data.state !== "定稿") {
                layer.msg("未处在该阶段，无法提交");
                return false;
            } else if (data.stageCheck === null || data.stageCheck.isPass === "不通过") {
                let page = layer.open({
                    type: 1
                    , offset: 10
                    , area: [700, 550]
                    , content: $("#paperUpload-box")
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        //数据回显
                        setMsg(data);
                    }
                    , end: function () {
                        $("#paperUpload-box").css("display", 'none');
                    }
                });
            } else if (data.stageCheck.state === "审核中") {
                layer.msg("已提交，不可重复提交");
                return false;
            }

        } else if (obj.event === 'query') {
            if (data.stageCheck === null) {
                layer.msg("暂无内容，无法查看");
            } else {
                layer.open({
                    type: 1
                    , offset: 50
                    , shade: [0.3, '#000']
                    , shadeClose: true
                    , skin: 'layui-layer-rim'
                    , area: ['740px', '600px']
                    , content: $("#paper-detail")
                    , title: '论文审核详情'
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        /*设置回显数据*/
                        setPaperDetail(data);
                    }
                    , end: function () {
                        $("#paper-detail").css("display", 'none');
                    }
                });
            }
        }
    });
    //监听终稿工具事件
    table.on('tool(paper-info3)', function (obj) {
        let data = obj.data;
        SNO = data.sno;
        state = '终稿';
        if (obj.event === 'taskDetail') {
            layer.open({
                type: 1
                , offset: 50
                , shade: [0.3, '#000']
                , shadeClose: true
                , skin: 'layui-layer-rim'
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '论文审核详情'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setTaskDetail(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        } else if (obj.event === 'edit') {
            if (data.state !== "终稿") {
                layer.msg("未处在该阶段，无法提交");
                return false;
            } else if (data.stageCheck === null || data.stageCheck.isPass === "不通过") {
                let page = layer.open({
                    type: 1
                    , offset: 10
                    , area: [700, 550]
                    , content: $("#paperUpload-box")
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        //数据回显
                        setMsg(data);
                    }
                    , end: function () {
                        $("#paperUpload-box").css("display", 'none');
                    }
                });
            } else if (data.stageCheck.state === "审核中") {
                layer.msg("已提交，不可重复提交");
                return false;
            }

        } else if (obj.event === 'query') {
            if (data.state !== "终稿" || data.stageCheck === null) {
                layer.msg("暂无内容，无法查看");
            } else {
                layer.open({
                    type: 1
                    , offset: 50
                    , shade: [0.3, '#000']
                    , shadeClose: true
                    , skin: 'layui-layer-rim'
                    , area: ['740px', '600px']
                    , content: $("#paper-detail")
                    , title: '论文审核详情'
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        /*设置回显数据*/
                        setPaperDetail(data);
                    }
                    , end: function () {
                        $("#paper-detail").css("display", 'none');
                    }
                });
            }
        }
    });

    /*审核附件上传*/
    upload.render({
        elem: '#test-upload-change'
        , url: '/uploadFile'
        , auto: false
        , field: 'file'
        , data: { //上传的额外参数
            SNO: function () {
                return SNO;
            },
            savePath: function () {
                if (state === "初稿") {
                    return 'student/firstDraft';
                } else if (state === "定稿") {
                    return 'student/finalize';
                } else if (state === "终稿") {
                    return 'student/finalVersion';
                }
            },
            fileName: function () {
                return SNO + ".doc";
            },
            type: 1,
            id: 1,//开题报告需要，此处防止空指针
            state: function () {
                return state;
            }
        }
        , size: 0 // 单位 KB 0（即不限制）
        , exts: "doc|docx"
        /*, accept: 'file'*/
        // ,acceptMime:'' 规定打开文件选择框时，筛选出的文件类型
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                //obj.resetFile(index, file, '重新命名的文件.jpg'); //重命名文件名，layui 2.3.0 开始新增
                let fileName = SNO + ".doc";
                obj.resetFile(index, file, fileName);
            });
        }
        , bindAction: '#test-upload-change-action'
        , done: function (res, index, upload) {
            //假设code=0代表上传成功
            layer.closeAll('page');
            layer.msg(res.msg);
            table.reload('paper-info1', {
                where: {userName: sno, state: "初稿", type: 1}
            });
            table.reload('paper-info2', {
                where: {userName: sno, state: "定稿", type: 1}
            });
            table.reload('paper-info3', {
                where: {userName: sno, state: "终稿", type: 1}
            });
        }
        , error: function () {
            //请求异常回调
            layer.msg('文件上传失败');
        }
    });

    //设置课题详情
    function setTaskDetail(data) {
        $("#task-tno").html(data.title1.tno);
        $("#task-sno").html(data.sno);
        $("#task-sname").html(data.sname);
        $("#task-from").html(data.title1.from);
        $("#task-id").html(data.title1.xtId);
        $("#task-title").html(data.title);
        $("#task-category").html(data.title1.categoty);
        $("#task-addtime").html(data.title1.addtime);
        $("#task-nature").html(data.title1.nature);
        $("#task-major").html(data.title1.majorName);
        $("#task-tname").html(data.title1.tName);
        $("#task-state").html(data.title1.state);
        $("#task-intr").html(data.title1.intr);
        $("#task-college").html(data.title1.collegeName);
    }

    //设置论文审核详情
    function setPaperDetail(data) {
        console.log(data);
        $("#paper-title").html(data.title);
        $("#paper-sno").html(data.sno);
        $("#paper-sname").html(data.sname);
        $("#paper-tno").html(data.title1.tno);
        $("#paper-tname").html(data.title1.tName);
        $("#paper-college").html(data.title1.collegeName);
        $("#paper-major").html(data.title1.majorName);
        $("#paper-state").html(data.stageCheck.state);
        $("#paper-isPass").html(data.stageCheck.isPass);
        $("#paper-opinion").html(data.stageCheck.opinion);
        if (data.stageCheck.sFileName === null || data.stageCheck.sFileName === '') {
            $("#stuFile").html('暂学生无附件');
        } else {
            let sFile = '<div class="layui-upload">' +
                '<span> ' + data.stageCheck.sFileName + '</span>   ' +
                '   <div class="layui-btn-group">' +
                '       <a  class="layui-btn layui-btn-normal" ' +
                '           href="javascript:POBrowser.openWindowModeless(\'../Word.jsp?fileName=' + data.stageCheck.sFileName + '&url=' + data.stageCheck.sFileUrl + '&type=2\', \'width=1200px;height=800px;\');">' +
                '           <i class="layui-icon layui-icon-link"></i>预览' +
                '       </a>' +
                '       <a  class="layui-btn" href="/downloadFile?filename=' + data.stageCheck.sFileName + '&url=' + data.stageCheck.sFileUrl + '"><i class="layui-icon layui-icon-upload"></i>下载</a>' +
                '    </div>' +
                '</div>';
            $("#stuFile").html(sFile);
        }


    }

    //设置编辑开题报告回显
    function setMsg(data) {
        //基本信息
        $("#sName").html(data.sname);
        $("#sNO").html(data.sno);
        $("#major").html(data.title1.majorName);
        $("#title").html(data.title);
    }

});